public class BinarySearchTree {
    class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public static TreeNode root;

    /**
     * 插入一个元素
     *
     * @param key
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);
        //如果根节点为空
        if (root == null) {
            root = node;
        }//根节点不为空
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            //比较它是大于根节点还是小于根节点
            if (cur.key < key) {
                parent = cur;
                cur = cur.right;
            } else if (cur.key > key) {
                parent = cur;
                cur = cur.left;
            } else {
                return false;//搜索二叉树无法插入相同的数据
            }
        }//此时cur为空
        //判断key的值是否大于parent
        if (parent.key < key) {
            parent.right = node;
        } else {
            parent.left = node;
        }
        return true;
    }

    //查找key是否存在
    public TreeNode search(int key) {
        //判断该二叉搜索树是空树
        if (root == null) {
            throw new EmptyTreeException("该树为空");
        }//不为空
        //判断是否大于根节点
        TreeNode cur = root;
        while (cur != null) {
            if (cur.key > key) {
                cur = cur.left;
            } else if (cur.key < key) {
                cur = cur.right;
            } else {
                return cur;
            }
        }
        return null;
    }

    //删除key的值
    public void remove(int key) {
        //判断该二叉树是否为空
        if (root == null) {
            throw new EmptyTreeException("该树为空，无法删除");
        }//树不为空，分三种情况；
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.key == key) {
                removeChild(parent, cur);
            } else if (cur.key < key) {
                parent = cur;
                cur = cur.right;
            } else {
                parent = cur;
                cur = cur.left;
            }
        }
    }

    public void removeChild(TreeNode parent, TreeNode cur) {
        //树不为空，分三种情况；
        //1.cur.left==null;
        if (cur.left == null) {
            if (cur == root) {
                cur = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {//2.cur.right==null
            if (cur == root) {
                cur = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {   //3.左右不为空
            //这里有一个叫做替罪羊,找右边的最小的，或者左边的最大的

            TreeNode index = cur.right;
            parent = cur;
            while (index.left != null) {
                parent = index;
                index = index.left;
            }//这时候找到了左边最小的
            cur.key = index.key;
            if (index == parent.left) {
                parent.left = index.right;
            } else {
                parent.right = index.right;//这里是while没有走进去
            }
        }

    }

    //打印二叉搜素树
    public void inorder(TreeNode root) {
        //中序遍历是有序的
        if (root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.key + " ");
        inorder(root.right);
    }
}
